package com.example.myapplication

import android.app.Activity
import android.content.Intent
import android.graphics.Color
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.provider.MediaStore
import android.view.View
import android.widget.ImageView
import android.widget.LinearLayout

class addnews : AppCompatActivity() {
    private lateinit var imageView1: ImageView
    private lateinit var imageView2: ImageView

    private val SELECT_IMAGE_REQUEST_CODE1 = 1
    private val SELECT_IMAGE_REQUEST_CODE2 = 2


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_addnews)

        imageView1 = findViewById(R.id.imageView7)
        imageView2 = findViewById(R.id.imageView8)
        imageView1.setOnClickListener {
            openGallery(SELECT_IMAGE_REQUEST_CODE1)
        }

        imageView2.setOnClickListener {
            openGallery(SELECT_IMAGE_REQUEST_CODE2)
        }

    }
    fun onImageViewClick(view: View) {

            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)

    }

    private fun openGallery(requestCode: Int) {
        val intent = Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI)
        startActivityForResult(intent, requestCode)
    }


    // 处理选取图片后的结果
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (resultCode == Activity.RESULT_OK && data != null) {
            when (requestCode) {
                SELECT_IMAGE_REQUEST_CODE1 -> {
                    val imageUri = data.data
                    imageView1.setImageURI(imageUri)
                }
                SELECT_IMAGE_REQUEST_CODE2 -> {
                    val imageUri = data.data
                    imageView2.setImageURI(imageUri)
                }
            }
        }
    }


}